<?php

/**
 * Class CartAction
 * 购物车
 */
class CartAction extends PublicAction
{
    protected $cartModel;
    protected $sessionId;
    protected $userId;

    function _initialize()
    {
        parent::_initialize();
        import("@.ORG.Online");
        new Online();
        $this->sessionId = cookie('onlineid');
        $this->userId = !empty($_SESSION['user']['id']) ? $_SESSION['user']['id'] : 0;
        $this->cartModel = M('Cart');
    }

    public function addToCart()
    {
        $id = I('post.id', 0, 'intval');
        $_SESSION['cart'][$id] = json_encode($_POST);

        $num = I('post.num', 1, 'intval');
        $modelid = 2;
        $modelname = $this->Model[$modelid]['tablename'];

        if (!$modelname) {
            $res['msg'] = 'error';
        }

        $r = M($modelname)->find($id);
        if ($this->userId) {
            $cart = $this->cartModel->where("product_id='{$id}' and user_id='{$this->userId}'")->find();
        } else {
            $cart = $this->cartModel->where("product_id='{$id}' and session_id='{$this->sessionId}'")->find();
        }

        if ($cart) {
            $cart['number'] = $cart['number'] + $num;
            $cart['price'] = $cart['product_price'] * $cart['number'];
            $this->cartModel->save($cart);
        } else {
            $data = array();
            $data['user_id'] = $this->userId;
            $data['session_id'] = $this->sessionId;
            $data['product_id'] = $r['id'];
            $data['product_thumb'] = $r['thumb'];
            $data['product_url'] = $r['url'];
            $data['product_name'] = $r['title'];
            $data['product_price'] = $r['price'];
            $data['modelid'] = $modelid;
            $data['number'] = $num;
            $data['is_checked'] = 1;
            $data['price'] = $data['product_price'] * $data['number'];
            $this->cartModel->add($data);
        }

        $data = array('status' => 1, 'info' => '已加入购物车', 'url' => '/cart');
        $this->ajaxReturn($data);
    }

    public function index()
    {
        if ($this->userId) {
            $this->cartModel->where("session_id='{$this->sessionId}'")->save(array('user_id' => $this->userId));
            $cart = $this->cartModel->where("user_id='{$this->userId}'")->select();
        } else {
            $cart = $this->cartModel->where("session_id='{$this->sessionId}'")->select();
        }

        $total = 0;
        foreach ($cart as $key => $val) {
            $total += $cart[$key]['price'];
        }

        $this->assign('total', $total);
        $this->assign('list', $cart);
        $this->display();
    }

    public function edit()
    {
        $id = I('id');
        $num = I('num');

        $_SESSION['cart'][$id] = json_decode($_SESSION['cart'][$id], ture);
        $_SESSION['cart'][$id]['num'] = $num;
        $_SESSION['cart'][$id]['subtotal'] = $num * $_SESSION['cart'][$id]['price'];

        $condition['user_id'] = $this->userId;
        $condition['session_id'] = $this->sessionId;
        $condition['product_id'] = $id;

        $data = $this->cartModel->where($condition)->find();
        $data['number'] = $num;
        $data['price'] = $data['product_price'] * $data['number'];
        $rs = $this->cartModel->save($data);
        $res['data'] = $rs ? 1 : 0;
        if ($res['data'])
            $_SESSION['cart'][$id] = json_encode($_SESSION['cart'][$id]);
    }

    public function del()
    {
        $id = $_POST['id'];
        $rs = $this->cartModel->delete($id);
        $res['data'] = $rs ? 1 : 0;
        unset($_SESSION['cart'][$id]);
        echo json_encode($res);
        exit;
    }

    public function clear()
    {
        unset($_SESSION['cart']);
        $this->redirect('product/index');
    }

    public function AsyncUpdateCart()
    {
        $cart = $_POST['cart'];

        foreach ($cart as $val) {
            $data['session_id'] = $this->sessionId;
            $data['number'] = $val['number'];
            $data['id'] = $val['id'];
            $data['is_checked'] = $val['selected'];
            $this->cartModel->save($data);
        }
        $curCart = M('Cart')->where(array('session_id' => $this->sessionId))->select();
        $data = array();
        foreach ($curCart as $val) {
            if ($val['is_checked']) {
                $data['number'] += $val['number'];

                $data['total_fee'] += $val['number'] * $val['product_price'];
            }
        }
        $data['total_fee'] = sprintf("%.2f ", $data['total_fee']);
        $this->ajaxReturn(['status' => 1, 'result' => $data]);
    }
}